package com.aplikata.evok.ui;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.DateSelectEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

import com.aplikata.evok.bo.DoctorSchedule;
import com.aplikata.evok.bo.PatientTherapy;
import com.aplikata.evok.bo.PatientTherapyComment;
import com.aplikata.evok.bo.PatientTherapyNode;
import com.aplikata.evok.bo.TherapyService;
import com.aplikata.evok.constants.EvokConstants;
import com.aplikata.evok.datamodel.TherapyAttachmentDataModel;
import com.aplikata.evok.datamodel.TherapyServiceDataModel;
import com.aplikata.util.FacesUtil;

/**
 * @ClassName: EvokPatientTherapyArchiveBean
 * @Description: TODO
 * @author Huyun
 * @date Sep 19, 2012 2:18:19 PM
 */
@ManagedBean(name = "evokPatientTherapyArchiveBean")
@ViewScoped
public class EvokPatientTherapyArchiveBean extends EvokPatientTherapyAbstractBean {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private LazyDataModel<PatientTherapy> therapyLazyModel;

	private Date startDateFrom;

	private Date startDateTo;

	private int tabDetailIndex;

	private List<PatientTherapyNode> pnList;
	
	private List<?> therapyList;

	private TherapyAttachmentDataModel attmodel;

	private TherapyServiceDataModel tsModel;

	private String priceTotal;

	@PostConstruct
	public void init() {
		super.init();
		patientTherapy = (PatientTherapy) FacesContext.getCurrentInstance().getExternalContext().getFlash()
				.get(EvokConstants.FLASH_PATIENT_THERAPY);
		if (patientTherapy != null) {
			tabIndex = 1;
			patientTherapy = (PatientTherapy) evokTherapyService.get(PatientTherapy.class, patientTherapy.getId());
			selectPatientTherapy(patientTherapy);
		}
		if (patientTherapy == null) {
			patientTherapy = new PatientTherapy(getSessionUser());
		}
	}

	public LazyDataModel<PatientTherapy> getTherapyLazyModel() {
		if (therapyLazyModel == null) {
			therapyLazyModel = new LazyDataModel<PatientTherapy>() {

				private static final long serialVersionUID = 1L;

				@Override
				public Object getRowKey(PatientTherapy therapy) {
					return therapy.getId();
				}

				@Override
				public PatientTherapy getRowData(String rowKey) {
					return (PatientTherapy) evokTherapyService.get(PatientTherapy.class, Long.parseLong(rowKey));
				}

				@Override
				public void setRowIndex(int rowIndex) {
					if (rowIndex == -1 || getPageSize() == 0) {
						super.setRowIndex(-1);
					} else
						super.setRowIndex(rowIndex % getPageSize());
				}

				@SuppressWarnings({ "unchecked", "rawtypes" })
				@Override
				public List<PatientTherapy> load(int first, int pageSize, String sortField, SortOrder sortOrder,
						Map<String, String> filters) {
					Object[] obj = evokTherapyService.getPatientTherapyArchive(startDateFrom, startDateTo, first,
							pageSize, sortField, sortOrder, filters);
					therapyLazyModel.setRowCount((Integer) obj[0]);
					return (List) obj[1];
				}
			};
		}

		return therapyLazyModel;
	}

	public void handleStartDateFromSelect(DateSelectEvent event) {
		startDateFrom = event.getDate();
		therapyLazyModel = null;
	}

	public void handleStartDateToSelect(DateSelectEvent event) {
		startDateTo = event.getDate();
		therapyLazyModel = null;
	}

	public void resetDates() {
		startDateFrom = null;
		startDateTo = null;
	}

	@SuppressWarnings({ })
	public void detailsAction(SelectEvent event) {
		patientTherapy = (PatientTherapy) event.getObject();
		selectPatientTherapy(patientTherapy);
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void selectPatientTherapy(PatientTherapy pt) {
		
		patientTherapy = pt;
		addTherapyToFlash();
		
		tabIndex = 1;
		tabDetailIndex = 0;
		
		therapyList = (List<PatientTherapyComment>) evokTherapyService.findByNamedQuery(
				PatientTherapy.QY_FIND_THERAPIES, new Object[] { patientTherapy.getId() });		
		
		Long roleId = patientTherapy.getCreator().getRole().getId();
		/*therapyTree = evokTherapyService.getTherapyTreeForDoctor(roleId, pt);*/
		pnList = evokTherapyService.getPatientTherapyNodeList(roleId, patientTherapy);
		commentList = (List<PatientTherapyComment>) evokTherapyService.findByNamedQuery(
				PatientTherapyComment.QY_FIND_BY_THERAPY, new Object[] { patientTherapy.getId() });

		attmodel = new TherapyAttachmentDataModel(evokTherapyService, evokTherapyService.getAttList(patientTherapy));

		List tslist = evokTherapyService.findByNamedQuery(TherapyService.QY_FIND_BY_PATIENT_THERAPY,
				new Object[] { patientTherapy.getId() });
		tsModel = new TherapyServiceDataModel(evokTherapyService, tslist);

		priceTotal = evokTherapyService.getPriceTotal(patientTherapy);

		therapyReservationList = (List<DoctorSchedule>) evokTherapyService.findByNamedQuery(
				DoctorSchedule.QY_FIND_PATIENT_THERAPY, new Object[] { patientTherapy.getId() });

	}

	public String cancelAction() {
		return "/pages/aplikata/evok/doctor/archive/patient_therapy_archive.xhtml";
	}

	public Date getStartDateFrom() {
		return startDateFrom;
	}

	public void setStartDateFrom(Date startDateFrom) {
		this.startDateFrom = startDateFrom;
	}

	public Date getStartDateTo() {
		return startDateTo;
	}

	public void setStartDateTo(Date startDateTo) {
		this.startDateTo = startDateTo;
	}

	public int getTabDetailIndex() {
		return tabDetailIndex;
	}

	public void setTabDetailIndex(int tabDetailIndex) {
		this.tabDetailIndex = tabDetailIndex;
	}

	public List<PatientTherapyNode> getPnList() {
		return pnList;
	}

	public void setPnList(List<PatientTherapyNode> pnList) {
		this.pnList = pnList;
	}

	public TherapyAttachmentDataModel getAttmodel() {
		return attmodel;
	}

	public void setAttmodel(TherapyAttachmentDataModel attmodel) {
		this.attmodel = attmodel;
	}

	public TherapyServiceDataModel getTsModel() {
		return tsModel;
	}

	public void setTsModel(TherapyServiceDataModel tsModel) {
		this.tsModel = tsModel;
	}

	public String getPriceTotal() {
		return priceTotal;
	}

	public void setPriceTotal(String priceTotal) {
		this.priceTotal = priceTotal;
	}

	public List<?> getTherapyList() {
		return therapyList;
	}

	public void setTherapyList(List<?> therapyList) {
		this.therapyList = therapyList;
	}

	public boolean isCommentAllow() {
		if (!FacesUtil.isKeyNull(patientTherapy.getId())
				&& FacesUtil.getUserFromSession().getId().equals(patientTherapy.getDomainPatient().getPatient().getUser().getId()))
			return true;
		return false;
	}
}
